package svc.elib.socnet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;

import svc.elib.db.Database;
import svc.elib.db.Paper;

/**
 * Constructs collaboration network restricted to a particular
 * journal present in digital library
 * 
 * @author svc
 * @date 16.6.2013
 */
public class JournalFilter {
	
	public class JournalNet {
		private Database db;
		private Net net;
		public JournalNet(Database db, Net net) {
			this.db = db;
			this.net = net;
		}
		
		public Database getDb() {
			return db;
		}
		
		public Net getNet() {
			return net;
		}
	}
	
	private LinkedList<JournalNet> jNets = new LinkedList<JournalNet>();
	
	public JournalFilter(Database db) {
		HashMap<String, LinkedList<Paper>> journalIndex = db.getJournalIndex();
		Iterator<Entry<String, LinkedList<Paper>>> it = journalIndex.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, LinkedList<Paper>> e = it.next();
			String journalName = e.getKey();
			LinkedList<Paper> papers = e.getValue();
			
			Database jDb = new Database(journalName, papers);
			SocConstructor sc = new SocConstructor(jDb);
			Net net = sc.getNet();
			jNets.addLast(new JournalNet(jDb, net));
		}
	}
	
	
	public void info() {
		System.out.println("Journal filter");
		System.out.println("---------------------------------------------------------");
		for (JournalNet n : jNets) {
			Database db = n.getDb();
			Net net = n.getNet();
			System.out.println("Journal: " + db.getName() + "\n");
			System.out.println("Database info");
			System.out.println(db.info());
			System.out.println("Network info");
			net.printInfo();
			System.out.println("\n\n");
		}
	}
	
	public LinkedList<JournalNet> getJournalNets() {
		return jNets;
	}
	
	public static void main(String[] args) 
		throws IOException
	{
		Database db = new Database("eLibData.csv", 1932, 2011);
		System.out.println(db.info());
		JournalFilter jf = new JournalFilter(db);
		jf.info();
		
		LinkedList<JournalNet> jNets = jf.getJournalNets();
		for (JournalNet jn : jNets) {
			EvolutionarySnapshots es = new EvolutionarySnapshots(jn.getNet());
			es.info();
		}
	}
}
